Page({
  data: {
      longitude: 0,
      latitude: 0,
      scale: 14,
      markers: [],
      selectedMarker: null,
      searchKeyword: ''
  },
  onLoad() {
      this.checkLocationPermission();
  },
  checkLocationPermission() {
      wx.getSetting({
          success: (res) => {
              if (!res.authSetting['scope.userLocation']) {
                  wx.authorize({
                      scope: 'scope.userLocation',
                      success: () => {
                          this.getLocation();
                      },
                      fail: () => {
                          wx.showModal({
                              title: '提示',
                              content: '需要获取您的位置信息才能展示附近景点，请授权',
                              success: (res) => {
                                  if (res.confirm) {
                                      wx.openSetting();
                                  }
                              }
                          });
                      }
                  });
              } else {
                  this.getLocation();
              }
          }
      });
  },
  getLocation() {
      wx.getLocation({
          type: 'gcj02',
          success: (res) => {
              console.log('获取到的经纬度信息:', res.longitude, res.latitude);
              this.setData({
                  longitude: res.longitude,
                  latitude: res.latitude
              }, () => {
                  this.getNearbyScenicSpots(res.longitude, res.latitude);
              });
          },
          fail: (err) => {
              console.error('获取位置失败', err);
              wx.showToast({
                  title: '获取位置失败，请检查设置',
                  icon: 'none'
              });
          }
      });
  },
  getNearbyScenicSpots(longitude, latitude) {
      const apiKey = 'GEABZ-PJZ6B-C6SU6-NNI7N-URZF6-ANFQ4'; // 替换为你自己的腾讯地图API Key
      console.log('使用的经纬度:', longitude, latitude);
      const url = `https://apis.map.qq.com/ws/place/v1/search?keyword=景点&boundary=nearby(${latitude},${longitude},1000)&page_size=10&key=${apiKey}`;
      console.log('请求 URL:', url);

      wx.request({
          url: url,
          success: (res) => {
              console.log('请求成功，返回数据:', res.data);
              if (res.data.status === 0) {
                  const pois = res.data.data;
                  const markers = pois.map(spot => {
                      return {
                          id: spot.id,
                          longitude: spot.location.lng,
                          latitude: spot.location.lat,
                          title: spot.title,
                          iconPath: '', // 需要替换为实际的图标路径
                          width: 30,
                          height: 30
                      };
                  });
                  this.setData({
                      markers
                  }, () => {
                      console.log('获取到的景点标记点数据:', markers);
                  });
              } else {
                  console.error('获取附近景点失败，错误信息:', res.data.message);
                  wx.showToast({
                      title: '获取附近景点失败',
                      icon: 'none'
                  });
              }
          },
          fail: (err) => {
              console.error('请求失败，错误信息:', err);
              wx.showToast({
                  title: '请求失败，请检查网络',
                  icon: 'none'
              });
          }
      });
  },
  onSearchInput(e) {
      this.setData({
          searchKeyword: e.detail.value
      });
  },
  onSearchConfirm() {
      const keyword = this.data.searchKeyword.trim().replace(/[^\w\u4e00-\u9fa5]/g, '').toLowerCase();
      console.log('搜索关键词:', keyword);
      if (!keyword) {
          wx.showToast({
              title: '请输入有效的搜索关键词',
              icon: 'none'
          });
          return;
      }
      const targetMarker = this.data.markers.find(marker => {
          const title = marker.title.replace(/[^\w\u4e00-\u9fa5]/g, '').toLowerCase();
          console.log('标记点标题:', title);
          return title.includes(keyword);
      });
      if (targetMarker) {
          this.setData({
              longitude: targetMarker.longitude,
              latitude: targetMarker.latitude,
              scale: 16
          });
      } else {
          wx.showToast({
              title: '未找到该景点',
              icon: 'none'
          });
      }
  },
  onMarkerTap(e) {
      const markerId = e.markerId;
      const selectedMarker = this.data.markers.find(marker => marker.id === markerId);
      this.setData({
          selectedMarker: {
              name: selectedMarker.title,
              address: selectedMarker.title // 这里应该根据实际数据修改
          }
      });
  }
});
  